import type { MenuOption } from "naive-ui";
import type { RouteRecordRaw } from "vue-router";

/** 静态路由 */
export const staticRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect/:path",
    component: () => import("@/views/redirect/Index.vue"),
    name: "redirect",
    meta: {
      auth: false,
      title: "redirect",
      translation: "redirect",
    },
  },
  {
    path: "/404",
    component: () => import("@/views/error/404.vue"),
    name: "404",
    meta: {
      auth: false,
      title: "404",
      translation: "404",
    },
  },
  {
    path: "/:pathMatch(.*)",
    name: "notFound",
    redirect: "/404",
  },
  {
    path: "/403",
    component: () => import("@/views/error/403.vue"),
    name: "403",
    meta: {
      auth: false,
      title: "403",
      translation: "403",
    },
  },
  {
    path: "/login",
    component: () => import("@/views/login/Login.vue"),
    name: "login",
    meta: {
      auth: false,
      title: "登录",
      translation: "common.login",
    },
  },
  {
    name: "dashboard",
    path: "/",
    redirect: "console",
    component: () => import("@/layout/Layout.vue"),
    meta: {
      auth: true,
      title: "控制台",
      translation: "common.console",
      icon: "ScaleOutline",
    },
    children: [
      {
        name: "console",
        path: "/console",
        component: () => import("@/views/dashboard/Console.vue"),
        meta: {
          auth: true,
          title: "仪表盘",
          translation: "common.dashboard",
          icon: "SpeedometerOutline",
        },
      },
    ],
  },
  {
    name: "personal",
    path: "/personal",
    redirect: "info",
    component: () => import("@/layout/Layout.vue"),
    meta: {
      auth: true,
      title: "common.console",
      icon: "ScaleOutline",
    },
    children: [
      {
        name: "info",
        path: "/info",
        component: () => import("@/views/personal/Info.vue"),
        meta: {
          auth: true,
          title: "common.dashboard",
          icon: "SpeedometerOutline",
        },
      },
    ],
  },
];

/** 需要拼接的静态路由 */
export const staticMenuList = ["dashboard"];

/** 生成静态菜单 */
export const genStaticMenu = () => {
  const list = staticRoutes.filter((item) => staticMenuList.includes(item.name as string));
  const res = staticMenu(list);
  return res;
};

export const staticMenu = (data: RouteRecordRaw[]) => {
  const result: MenuOption[] = [];

  data.forEach((item) => {
    const menu: MenuOption = {
      translation: item?.meta?.translation,
      label: item?.meta?.title,
      show: true,
      // @ts-ignore
      key: item.name,
      iconName: item?.meta?.icon,
    };
    if (item.children) {
      menu.children = staticMenu(item.children);
    }
    result.push(menu);
  });
  return result;
};
